﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CMS
{
    public partial class _index : System.Web.UI.Page
    {
        public SqlDataReader dr = null;

        public FenYe fenye = new FenYe();

        string url = "index.aspx";

        public SqlHelper sqlHelper = new SqlHelper();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["stu_name"] != null)
            {
                this.stu_name.InnerHtml = Session["stu_name"].ToString();
            }

            //获取用户名
            if (Session["stu_no"] != null)
            {
                this.stu_no.InnerText = Session["stu_no"].ToString();
            }
            //else
            //{
            //    Response.Redirect("log.aspx");
            //}

            if (Request.QueryString["pageNow"] != null) {
                fenye.pageNow = int.Parse(Request.QueryString["pageNow"]);
            }

            fenye.pageSize = 2;

            string pageNow = fenye.pageNow.ToString();
            string pageSize = fenye.pageSize.ToString();
            string notFound = ((fenye.pageNow - 1) * fenye.pageSize).ToString();

            string sql1 = "SELECT   COUNT(*) FROM gonggao";
            StringBuilder sb = new StringBuilder(1024);
            sb.AppendFormat("SELECT   TOP ({0}) info_title, info_content, info_publisher, info_datetime ",pageSize);
            sb.Append("FROM      gonggao ");
            sb.Append("WHERE   (info_id NOT IN ");
            sb.AppendFormat("                    (SELECT   TOP ({0}) info_id ",notFound);
            sb.Append("                     FROM      gonggao AS gonggao_1 ORDER BY info_id DESC)) ORDER BY info_id DESC");

            sqlHelper.fenye_query(sql1, sb.ToString(), url, fenye);


            dr = fenye.dr;
            //while (dr.Read())
            //{
            //    Response.Write(dr[0].ToString());
            //}
        }

        protected void send_Click(object sender, EventArgs e)
        {
            string tbtitle = "";
            string tbcontent = "";

            sqlHelper = new SqlHelper();

            if(this.tbtitle.Text.Length>25){
                Response.Write("<script>alert('标题不能超过25个字')</script>");
                return;
            }else{
                tbtitle = this.tbtitle.Text;;
            }

            if (this.tbcontent.Text.Length > 200)
            {
                Response.Write("<script>alert('内容不能超过200个字')</script>");
                return;
            }
            else {
                tbcontent = this.tbcontent.Text;
            }
            
            DateTime dt = System.DateTime.Now;
            string datetime = dt.ToString("yyyy-MM-dd HH:mm:ss");

            string sql = "INSERT INTO gonggao(info_title, info_content, info_publisher, info_datetime) VALUES   (N'" + tbtitle + "', N'" + tbcontent + "', N'" + Session["stu_name"].ToString() + "', N'"+datetime+"')";
            //String.Format(sql, tbtitle, tbcontent, Session["stu_name"].ToString(), "2016-11-11");

            if (sqlHelper.ExecuteNonQuery(sql) > 0)
            {
                Response.Write("<script>alert('发送成功');location.href='index.aspx'</script>");
            }
            else {
                Response.Write("<script>alert('发送失败');</script>");
            }
            
        }

    }
}